package com.safe.app.controller.user;

import com.safe.app.config.ResultBean;
import com.safe.app.config.context.RequestBaseEntity;
import com.safe.app.config.oos.CloudStorageServiceImpl;
import com.safe.app.controller.BaseController;
import com.safe.app.model.Depart;
import com.safe.app.model.User;
import com.safe.app.service.UserService;
import com.safe.app.utils.common.AESUtils;
import com.safe.app.utils.common.RegxUtil;
import com.safe.app.utils.common.UUIDGenerator;
import com.safe.app.utils.exceptions.base.BadRequestException;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 用户管理控制层
 *
 * @author h2l
 */
@Controller
@Slf4j
@RequestMapping("/uc")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private CloudStorageServiceImpl cloudStorageService;


    /**
     * @return 分页加载用户列表
     * @throws Exception
     * @author h2l
     */
    @RequestMapping("getUserList")
    @ResponseBody
    public ResultBean getUserList(@RequestBody JSONObject jsonObject) throws Exception {
        Integer pageNo = jsonObject.optInt("pageNo");
        Integer pageSize = jsonObject.optInt("pageSize");
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;
        List<User> useList = userService.getUseList(pageNo, pageSize);
        return new ResultBean(useList);
    }


    /**
     * 文件上传入口
     *
     * @return 上传结果：返回结果 上传文件等url
     */
    @RequestMapping("uploadFile")
    @ResponseBody
    public ResultBean uploadFile(@RequestParam("file") MultipartFile uploadFile) throws Exception {
        if (uploadFile.isEmpty()) {
            throw new BadRequestException("上传文件不能为空");
        }
        //防止文件名重复，用uuid来定义
        String suffix = UUIDGenerator.getUUID();
        String uploadUrl = cloudStorageService.upload(uploadFile.getBytes(), suffix);
        return new ResultBean(uploadUrl, "文件上传成功");
    }


    /**
     * @return 新增用户
     * @throws Exception
     * @author h2l
     */
    @RequestMapping("addUser")
    @ResponseBody
    public ResultBean addUser(@RequestBody RequestBaseEntity<User> baseEntity) throws Exception {
        if (baseEntity == null) {
            throw new BadRequestException();
        }
        if (!RegxUtil.matcherChinese(baseEntity.getUserName())) {
            throw new BadRequestException("用户名格式不正确，只能是中文、数字、字母、下划线等组合");
        }
        if (!RegxUtil.checkMobile(baseEntity.getPhoneNumber())) {
            throw new BadRequestException("手机号码格式不正确！");
        }
        if (userService.checkOnlyPhone(baseEntity.getPhoneNumber())) {
            throw new BadRequestException("手机号码已经存在！");
        }
        User user = new User();
        user.setEmployeeCode(baseEntity.getEmployeeCode());
        user.setUserName(baseEntity.getUserName());
        user.setPassword(AESUtils.encode(baseEntity.getPassword()));
        user.setDepartId(baseEntity.getDepartId());
        user.setEmail(baseEntity.getEmail());
        user.setPhoneNumber(baseEntity.getPhoneNumber());
        user.setRemark(baseEntity.getRemark());
        boolean b = userService.addUser(user);
        return new ResultBean(b);
    }

    /**
     * 部门列表（暂无父子级别关系）
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("getDepartList")
    @ResponseBody
    public ResultBean listDeparts() throws Exception {
        List<Depart> departList = userService.getDepartList();
        return new ResultBean(departList);
    }

    /**
     * 部门列表（暂无父子级别关系）
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("addDepart")
    @ResponseBody
    public ResultBean addDepart() throws Exception {
        List<Depart> departList = userService.getDepartList();
        return new ResultBean(departList);
    }

}
